<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="DB," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="数据库中，树结构的实现在关系型数据库中，我们通过为每条记录添加一个parentId字段的方式，来实现无限扩展的树结构。既然有了树结构，我们也有需求如何查询某个节点下的所有子节点，包括除直接子节点之外的所有的孙子节点。这就需要使用递归来实现。那么各种数据库是否都有这种实现的语法呢？今天，我们来探讨下Mysql和Oracle的实现。 建表(Mysql)CREATE TABLE `treenodes`">
<meta name="keywords" content="DB">
<meta property="og:type" content="article">
<meta property="og:title" content="数据库的递归查询">
<meta property="og:url" content="https://gitee.com/shuhao/2018/01/05/数据库的递归查询/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="数据库中，树结构的实现在关系型数据库中，我们通过为每条记录添加一个parentId字段的方式，来实现无限扩展的树结构。既然有了树结构，我们也有需求如何查询某个节点下的所有子节点，包括除直接子节点之外的所有的孙子节点。这就需要使用递归来实现。那么各种数据库是否都有这种实现的语法呢？今天，我们来探讨下Mysql和Oracle的实现。 建表(Mysql)CREATE TABLE `treenodes`">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://gitee.com/blog/images/DB/recursion_table.png">
<meta property="og:image" content="https://gitee.com/blog/images/DB/group_concat.jpg">
<meta property="og:image" content="https://gitee.com/blog/images/DB/recursion_oracle_result.jpg">
<meta property="og:image" content="https://gitee.com/blog/images/DB/recursion_oracle_result_2.jpg">
<meta property="og:updated_time" content="2018-04-16T07:44:27.536Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据库的递归查询">
<meta name="twitter:description" content="数据库中，树结构的实现在关系型数据库中，我们通过为每条记录添加一个parentId字段的方式，来实现无限扩展的树结构。既然有了树结构，我们也有需求如何查询某个节点下的所有子节点，包括除直接子节点之外的所有的孙子节点。这就需要使用递归来实现。那么各种数据库是否都有这种实现的语法呢？今天，我们来探讨下Mysql和Oracle的实现。 建表(Mysql)CREATE TABLE `treenodes`">
<meta name="twitter:image" content="https://gitee.com/blog/images/DB/recursion_table.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2018/01/05/数据库的递归查询/"/>





  <title> 数据库的递归查询 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2018/01/05/数据库的递归查询/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                数据库的递归查询
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-05T19:11:19+08:00">
                2018-01-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/DB/" itemprop="url" rel="index">
                    <span itemprop="name">DB</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="数据库中，树结构的实现"><a href="#数据库中，树结构的实现" class="headerlink" title="数据库中，树结构的实现"></a>数据库中，树结构的实现</h3><p>在关系型数据库中，我们通过为每条记录添加一个parentId字段的方式，来实现无限扩展的树结构。既然有了树结构，我们也有需求如何查询某个节点下的所有子节点，包括除直接子节点之外的所有的孙子节点。这就需要使用递归来实现。那么各种数据库是否都有这种实现的语法呢？今天，我们来探讨下Mysql和Oracle的实现。</p>
<h3 id="建表-Mysql"><a href="#建表-Mysql" class="headerlink" title="建表(Mysql)"></a>建表(Mysql)</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE `treenodes` ( </span><br><span class="line"></span><br><span class="line">  `id` int(11) NOT NULL, </span><br><span class="line"></span><br><span class="line">  `nodename` varchar(20) DEFAULT NULL, </span><br><span class="line"></span><br><span class="line">  `pid` int(11) DEFAULT NULL, </span><br><span class="line"></span><br><span class="line">  PRIMARY KEY (`id`) </span><br><span class="line"></span><br><span class="line">) ENGINE=InnoDB DEFAULT CHARSET=latin1;</span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;1&apos;, &apos;A&apos;, &apos;0&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;2&apos;, &apos;B&apos;, &apos;1&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;3&apos;, &apos;C&apos;, &apos;1&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;4&apos;, &apos;D&apos;, &apos;2&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;5&apos;, &apos;E&apos;, &apos;2&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;6&apos;, &apos;F&apos;, &apos;3&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;7&apos;, &apos;G&apos;, &apos;6&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;8&apos;, &apos;H&apos;, &apos;0&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;9&apos;, &apos;I&apos;, &apos;8&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;10&apos;, &apos;J&apos;, &apos;8&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;11&apos;, &apos;K&apos;, &apos;8&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;12&apos;, &apos;L&apos;, &apos;9&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;13&apos;, &apos;M&apos;, &apos;9&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;14&apos;, &apos;N&apos;, &apos;12&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;15&apos;, &apos;O&apos;, &apos;12&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;16&apos;, &apos;P&apos;, &apos;15&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;17&apos;, &apos;Q&apos;, &apos;15&apos;);</span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;18&apos;, &apos;R&apos;, &apos;3&apos;);</span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;19&apos;, &apos;S&apos;, &apos;2&apos;);</span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;20&apos;, &apos;T&apos;, &apos;6&apos;);</span><br><span class="line"></span><br><span class="line">INSERT INTO `treenodes` VALUES (&apos;21&apos;, &apos;U&apos;, &apos;8&apos;);</span><br></pre></td></tr></table></figure>
<img src="/blog/images/DB/recursion_table.png">
<h3 id="Mysql实现递归查询"><a href="#Mysql实现递归查询" class="headerlink" title="Mysql实现递归查询"></a>Mysql实现递归查询</h3><h6 id="1、group-concat-："><a href="#1、group-concat-：" class="headerlink" title="1、group_concat()："></a>1、group_concat()：</h6><p>该函数的作用是，多条记录合并成一条记录。多用在group by 中，将每个组内的数据合并成有一个数据。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select pid, GROUP_CONCAT(id)as &apos;相同pid中所有子节点的id&apos; , GROUP_CONCAT(nodename) as &apos;相同pid中包含的所有节点名&apos; from treenodes group by pid</span><br></pre></td></tr></table></figure></p>
<img src="/blog/images/DB/group_concat.jpg">
<p>有了这个函数，我们就可以查询出某个节点下所有子节点的id了。然后我们是不是又想到了另一个mysql的操作符，对没错，就是<strong><em>in</em></strong>。但是，group_concat函数返回的值符不符合in的要求呢，我们又要如何操作？</p>
<h6 id="2、find-in-set"><a href="#2、find-in-set" class="headerlink" title="2、find_in_set"></a>2、find_in_set</h6><p>为了解决，<strong><em>in</em></strong>操作符需要数组的问题，mysql提供了一个新的函数，find_in_set用来在多条记录中查找特定列<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select GROUP_CONCAT(id) as &apos;直接子节点的id&apos; from treenodes where FIND_IN_SET(pid,&apos;1&apos;)</span><br></pre></td></tr></table></figure></p>
<img class="images/DB/find_in_set.jpg">
<p>这条语句查询出来的是pid为1的所有的子节点，当我们拿到结果后再执行该语句，则查出的是第二层的节点，如此递归下去，便可以查询所有的子节点。那么我们如何实现这个递归呢？答案当然是Mysql中的函数了。</p>
<h6 id="3、Mysql函数定义"><a href="#3、Mysql函数定义" class="headerlink" title="3、Mysql函数定义"></a>3、Mysql函数定义</h6><p>先了解<a href="http://www.cnblogs.com/zhangminghui/p/4113160.html" target="_blank" rel="external">Mysql如何定义函数</a>。我们来写我们这个递归函数<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">delimiter //</span><br><span class="line">create function &apos;getChildList&apos;(rootId INT)</span><br><span class="line">	return char(255)</span><br><span class="line">begin</span><br><span class="line">	declare result char(255);</span><br><span class="line">	declare rootIdStr = char(255);</span><br><span class="line"></span><br><span class="line">	set result = &apos;&apos;;</span><br><span class="line">	set rootIdStr = cast(rootId as char);</span><br><span class="line"></span><br><span class="line">	--递归来了</span><br><span class="line">	while rootIdStr is not null do</span><br><span class="line">		result = concat(result, &quot;,&quot;, rootIdStr);</span><br><span class="line">		select group_concat(id) into rootIdStr from treenodes where find_in_set(pid, rootIdStr) &gt; 0;</span><br><span class="line">	end while</span><br><span class="line">	return result;</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">--调用函数</span><br><span class="line">select getChildList(1) as &apos;1的所有子节点id&apos; ;</span><br></pre></td></tr></table></figure></p>
<img class="images/DB/recursion_mysql_result.jpg">
<p>好了，我们递归的函数写完了。主要使用了一个<strong><em>while</em></strong>循环，还有一个<strong><em>into</em></strong>关键字和我们之前提到的两个函数<strong><em>group_concat</em></strong>,<strong><em>find_in_set</em></strong>。</p>
<h3 id="Oracle实现递归"><a href="#Oracle实现递归" class="headerlink" title="Oracle实现递归"></a>Oracle实现递归</h3><p>Oracle的练习和实验，我们使用在线Oracle平台：(<a href="https://livesql.oracle.com" target="_blank" rel="external">https://livesql.oracle.com</a>) 。（不过貌似需要翻墙^~~^）<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE treenodes ( </span><br><span class="line"></span><br><span class="line">  id NUMBER NOT NULL, </span><br><span class="line"></span><br><span class="line">  nodename varchar(20) DEFAULT NULL, </span><br><span class="line"></span><br><span class="line">  pid NUMBER DEFAULT NULL, </span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">) </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (1, &apos;A&apos;, 0); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (2, &apos;B&apos;, 1); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (3, &apos;C&apos;, 1); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (4, &apos;D&apos;, 2); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (5, &apos;E&apos;, 2); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (6, &apos;F&apos;, 3); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (7, &apos;G&apos;, 6); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (8, &apos;H&apos;, &apos;0&apos;); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (9, &apos;I&apos;, 8); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (10, &apos;J&apos;, 8); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (11, &apos;K&apos;, 8); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (12, &apos;L&apos;, 9); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (13, &apos;M&apos;, 9); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (14, &apos;N&apos;, 12); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (15, &apos;O&apos;, 12); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (16, &apos;P&apos;, 15); </span><br><span class="line"></span><br><span class="line">INSERT INTO treenodes VALUES (17, &apos;Q&apos;, 15);</span><br></pre></td></tr></table></figure></p>
<h6 id="查询所有的子节点"><a href="#查询所有的子节点" class="headerlink" title="查询所有的子节点"></a>查询所有的子节点</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select * from treenodes</span><br><span class="line"> start with id=1</span><br><span class="line"> connect by prior id = pid</span><br></pre></td></tr></table></figure>
<img src="/blog/images/DB/recursion_oracle_result.jpg">
<p>有没有很惊喜，Oracle一条语句就能解决</p>
<h6 id="从该节点往上查找所有的父节点"><a href="#从该节点往上查找所有的父节点" class="headerlink" title="从该节点往上查找所有的父节点"></a>从该节点往上查找所有的父节点</h6><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select * from treenodes</span><br><span class="line"> start with id=17</span><br><span class="line"> connect by prior pid = id</span><br></pre></td></tr></table></figure>
<img src="/blog/images/DB/recursion_oracle_result_2.jpg">
<p>Oracle查询所有子节点还是十分简单，通过 <strong><em>start with… connect by prior</em></strong>就能解决。</p>
<blockquote>
<p>参考<br><a href="http://www.th7.cn/db/mysql/201709/254864.shtml" target="_blank" rel="external">Mysql递归查询所有子节点</a><br><a href="http://www.iteye.com/topic/287749" target="_blank" rel="external">Oracle实现递归查询</a><br><a href="https://livesql.oracle.com/apex/f?p=590:1:106766492687176::NO:::" target="_blank" rel="external">Oracle在线工具</a></p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/DB/" rel="tag"># DB</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/26/logstash安装/" rel="next" title="logstash安装">
                <i class="fa fa-chevron-left"></i> logstash安装
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/01/06/python随记/" rel="prev" title="python随记">
                python随记 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据库中，树结构的实现"><span class="nav-number">1.</span> <span class="nav-text">数据库中，树结构的实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#建表-Mysql"><span class="nav-number">2.</span> <span class="nav-text">建表(Mysql)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mysql实现递归查询"><span class="nav-number">3.</span> <span class="nav-text">Mysql实现递归查询</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1、group-concat-："><span class="nav-number">3.0.0.1.</span> <span class="nav-text">1、group_concat()：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2、find-in-set"><span class="nav-number">3.0.0.2.</span> <span class="nav-text">2、find_in_set</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3、Mysql函数定义"><span class="nav-number">3.0.0.3.</span> <span class="nav-text">3、Mysql函数定义</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Oracle实现递归"><span class="nav-number">4.</span> <span class="nav-text">Oracle实现递归</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#查询所有的子节点"><span class="nav-number">4.0.0.1.</span> <span class="nav-text">查询所有的子节点</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#从该节点往上查找所有的父节点"><span class="nav-number">4.0.0.2.</span> <span class="nav-text">从该节点往上查找所有的父节点</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
